<template>
  <div class="container">
    <div class="login">
      <h2>叩丁狼商品管理系统</h2>
      <el-form
        ref="ruleFormRef"
        :model="ruleForm"
        status-icon
        :rules="rules"
        class="demo-ruleForm"
      >
        <el-form-item prop="username">
          <el-input
            v-model="ruleForm.username"
            autocomplete="off"
            placeholder="请输入账号"
          >
            <template #prefix>
              <el-icon class="el-input__icon" size="24"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="ruleForm.password"
            type="password"
            autocomplete="off"
            placeholder="请输入密码"
            @keyup.enter="submitForm(ruleFormRef)"
          >
            <template #prefix>
              <el-icon class="el-input__icon" size="24"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>

        <el-form-item>
          <el-button
            type="primary"
            @click="submitForm(ruleFormRef)"
            :loading="login"
          >
            提交
          </el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script setup lang="ts">
import { ApiLogin } from './api'
import { Lock, User } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import { FormInstance, ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

const ruleFormRef = ref<FormInstance>()

const router = useRouter()

const login = ref<boolean>(false)
console.log(router.getRoutes())
// 自定义校验规则
const valiDateUsername = (rule: any, value: any, callback: any) => {
  if (value === '') {
    callback(new Error('请输入密码'))
  } else if (value.length < 6) {
    callback(new Error('密码不能小于6位数'))
  } else {
    callback()
  }
}

// 数据绑定
const ruleForm = reactive({
  username: '',
  password: '',
})

// 表单规则
const rules = reactive({
  username: [{ required: true, message: '请输入账号', trigger: 'blur' }],
  password: [{ validator: valiDateUsername, trigger: 'blur' }],
})

// 提交按钮
const submitForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      login.value = true
      // 发起登录请求
      ApiLogin(ruleForm)
        .then((res) => {
          if (res.code === 200) {
            // 存token
            localStorage.setItem('token', res.data.tokenHead + res.data.token)
            router.push('/index')
            ElMessage.success('登录成功！')
          } else {
            login.value = false
          }
        })
        .catch(() => {
          login.value = true
        })
    } else {
      ElMessage.warning('请检查账号密码！')

      return false
    }
  })
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
  background-color: #2d3a4b;
  .login {
    width: 420px;
    height: 250px;
    position: fixed;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    h2 {
      color: #fff;
      text-align: center;
      margin: 10px 0;
    }

    :deep(.el-input__wrapper) {
      background-color: #283443;
    }
    :deep(.el-input__inner) {
      height: 50px;
      color: #fff;
    }

    :deep(.el-button) {
      width: 100%;
      height: 40px;
    }
  }
}
</style>
